package com.example.demo.service.impl;

import com.example.demo.dao.UserMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.User;
import com.example.demo.service.IUserService;
import com.example.demo.util.MD5Util;
import com.example.demo.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public void insertUser(String username,String password) {

        if(username==null||!StringUtils.hasLength(username)||password==null||!StringUtils.hasLength(password)){
        throw new ApplicationException("参数校验失败");
        }
             User user = userMapper.selectUserByUserName(username);
        if(user!=null){
            throw new ApplicationException("用户已经存在");
        }
        User newUser = new User();
        newUser.setUsername(username);
        String salt = UUIDUtil.getUUID_32();
        newUser.setPassword(MD5Util.md5Last(password,salt));
        newUser.setSalt(salt);
        newUser.setScore(1000L);
        newUser.setTotalCount(0L);
        newUser.setWinCount(0L);
        int flag =userMapper.insert(newUser);
        if(flag!=1){
            throw new ApplicationException("操作失败");
        }

    }
    @Override
    public boolean login(String username, String password) {

        if(username==null||!StringUtils.hasLength(username)||password==null||!StringUtils.hasLength(password)){
            throw new ApplicationException("参数校验失败");
        }
        User user = userMapper.selectUserByUserName(username);
        if(user==null||user.getUserId()<=0){
            throw new ApplicationException("用户不存在");
        }
        String salt = user.getSalt();
        if(!MD5Util.md5Last(password,salt).equals(user.getPassword())){
            throw new ApplicationException("密码错误");
        }
        return true;
    }

    @Override
    public User selectUserByName(String username) {
           if(username==null||!StringUtils.hasLength(username)){
               throw new ApplicationException("参数校验失败");
           }

           User user = userMapper.selectUserByUserName(username);
           if(user==null||user.getUserId()<=0){
               throw new ApplicationException("用户不存在");
           }
           return user;
    }

    @Override
    public User selectUserById(Long userId) {
        if(userId==null||userId<=0){
            throw new ApplicationException("参数校验失败");
        }

        User user = userMapper.selectUserById(userId);
        if(user==null||user.getUserId()<=0){
            throw new ApplicationException("用户不存在");
        }
        log.info("这里的User为"+user);
        return user;

    }

    @Override
    public void updateWinnerUserById(Long userId) {
        if(userId==null||userId<=0){
            throw new ApplicationException("参数校验失败");
        }
        User user = userMapper.selectUserById(userId);
        if(user==null||user.getUserId()<=0){
            throw new ApplicationException("用户不存在");
        }
        int flag = userMapper.updateWinnerUserById(userId);
        if(flag!=1){
            throw new ApplicationException("操作失败");
        }

    }

    @Override
    public void updateLoserUserById(Long userId) {
        if(userId==null||userId<=0){
            throw new ApplicationException("参数校验失败");
        }
        User user = userMapper.selectUserById(userId);
        if(user==null||user.getUserId()<=0){
            throw new ApplicationException("用户不存在");
        }
        int flag = userMapper.updateLoserUserById(userId);
        if(flag!=1){
            throw new ApplicationException("操作失败");
        }
    }
}
